//
//  YKWevViewController.m
//  baisi
//
//  Created by 吴高烽 on 2016/10/29.
//  Copyright © 2016年 吴高烽. All rights reserved.
//

#import "YKWebViewController.h"
#import <WebKit/WebKit.h>
@interface YKWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
@property (weak, nonatomic) IBOutlet UIView *webView;
@property (nonatomic ,strong)UIProgressView *progressView;
@property (nonatomic ,weak)WKWebView *wkWeb;
@end

@implementation YKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatProgressView];
    [self creatWebView];
  
}
-(void)creatWebView{
    WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
    configuration.preferences = [WKPreferences new];
    configuration.preferences.minimumFontSize = 10 ;
    configuration.preferences.javaScriptEnabled = YES ;
    configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES ;
    
    WKWebView *webView = [[WKWebView alloc]initWithFrame:self.webView.bounds configuration:configuration];
    self.wkWeb = webView ;
    [self.webView addSubview:webView];
    webView.navigationDelegate = self ;
    webView.UIDelegate = self ;
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [webView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
}
-(void)viewDidLayoutSubviews{
    self.wkWeb.frame = self.webView.bounds ;
}
-(void)creatProgressView{
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.height-2, self.view.width, 2)];
    self.progressView.backgroundColor = [UIColor redColor];
    [self.navigationController.navigationBar addSubview:self.progressView];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"estimatedProgress"]){
        //estimatedProgress取值范围是0-1;
        self.progressView.progress = self.wkWeb.estimatedProgress;
    }
    if (!self.wkWeb.loading) {
        [UIView animateWithDuration:0.5 animations:^{
            NSLog(@"1111111");
            self.progressView.alpha = 0;
        }];
    }

}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{

}
-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    [UIView animateWithDuration:0.5 animations:^{
        self.progressView.alpha = 0;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    [self.wkWeb removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.wkWeb removeObserver:self forKeyPath:@"loading"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
